import {
	MultiEditViewPanelActionType,
	ICtrlDataAction,
	ICtrlEvent,
	IMultiEditViewPanelAbility,
	IMultiEditViewPanelControllerParams,
	IMultiEditViewPanelModel,
	IMultiEditViewPanelStore,
	IParam,
	IMultiEditViewPanelController,
} from '@/core';
import { QXEvent } from 'qx-util';
import { PropType } from 'vue';
import {
	getCtrlClassNames,
	useCtrlAction,
	useCtrlBasic,
	useCtrlControllerParams,
	useCtrlProps,
	handleCtrlAction,
	handleCtrlInit,
	handleCtrlMounted,
} from '../use-ctrl';

/**
 * 多编辑面板输入参数
 *
 * @export
 * @return {*}
 */
export function useMultiEditViewPanelProps() {
	return {
		...useCtrlProps(),
		newView: Function,
		embedView: {
			type: Object as PropType<IParam>,
			default: {},
		},
		panelStyle: {
			type: String as PropType<'ROW' | 'TAB_TOP'>,
			default: 'ROW',
		},
		actions: Object as PropType<ICtrlDataAction>,
		selectFirstDefault: Boolean,
	};
}

/**
 * 多编辑面板基础参数
 *
 * @export
 * @param {IMultiEditViewPanelModel} model
 * @param {IParam} props
 * @return {*}
 */
export function useMultiEditViewPanelBasic(
	model: IMultiEditViewPanelModel,
	props: IParam
) {
	const getClassNames = (props: IParam) => {
		const names = getCtrlClassNames(model, props);
		return names;
	};
	return {
		...useCtrlBasic(model, props),
		getClassNames,
		handleCtrlAction,
		handleCtrlInit,
		handleCtrlMounted,
	};
}

/**
 * 多编辑面板部件行为
 *
 * @export
 */
export function useMultiEditViewPanelAction(
	controller: IMultiEditViewPanelController<
		IMultiEditViewPanelStore,
		IMultiEditViewPanelAbility
	>
) {
	const handleAdd = (event: MouseEvent) => {
		controller.handleAdd();
	};

	const handleRemove = (item: IParam) => {
		controller.handleRemove(item);
	};
	const handleSelectionChange = (selections: IParam[]) => {
		controller.handleSelectionChange(selections);
	};
	return {
		...useCtrlAction(controller),
		handleSelectionChange,
		handleRemove,
		handleAdd,
	};
}

/**
 * 多编辑面板部件控制器
 *
 * @export
 */
export function useMultiEditViewPanelControllerParams<
	S extends IMultiEditViewPanelStore,
	M extends IMultiEditViewPanelModel
>(
	model: M,
	props: any,
	evt: QXEvent<
		ICtrlEvent<MultiEditViewPanelActionType, IMultiEditViewPanelAbility>
	>,
	otherParams: IParam = {}
): IMultiEditViewPanelControllerParams<
	MultiEditViewPanelActionType,
	IMultiEditViewPanelAbility
> {
	return {
		...useCtrlControllerParams<S, M>(model, props, evt, otherParams),
		actions: props.actions,
		selectFirstDefault: props.selectFirstDefault,
		openView: props.openView,
		newView: props.newView,
		embedView: props.embedView,
	};
}
